package com.xiaobai.clientapp;

import com.xiaobai.clientapp.lang.ApiResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class TestController {

    @Resource
    private SpikeProvider spikeProvider;

    @Value("${oauth2.client_id}")
    private String client_id;
    @Value("${oauth2.client_secret}")
    private String client_secret;
    /**
     * 客户端根据code， client_id， client_secret获取 token
     * @return
     */
    @PostMapping("/oauth2/callback")
    public ApiResponse<TokenResponse> callback(@Validated @RequestBody CallbackRequest callbackRequest) {
        TokenRequest tokenRequest = new TokenRequest();
        tokenRequest.setClient_id(client_id);
        tokenRequest.setClient_secret(client_secret);
        tokenRequest.setRedirect_uri(callbackRequest.getRedirect_uri());
        tokenRequest.setCode(callbackRequest.getCode());
        return spikeProvider.getToken(tokenRequest);
    }
}
